//
//  NewFeatureViewController.m
//  MyProject
//
//  Created by 石小强 on 2018/3/20.
//  Copyright © 2018年 yanjiaming. All rights reserved.
//

#import "NewFeatureViewController.h"
#import "AppDelegate.h"
#import "NSUDManager.h"
#import "LoginViewController.h"
#import "SelectRolesViewController.h"
#import "RolesViewController.h"

#define NewFeatureCount 3

@interface NewFeatureViewController ()

@end

@implementation NewFeatureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.scrollView.contentSize = CGSizeMake(kScreenWidth * NewFeatureCount, 0);
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    
    [self addImageToView];
}


#pragma mark - 添加图片
- (void)addImageToView {
    for (NSInteger i = 0; i < NewFeatureCount; i++) {
        UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake(i * kScreenWidth, 0, kScreenWidth, kScreenHeight)];
        if ([Tool isIphoneX]) {
            imgV.image = [UIImage imageNamed:[NSString stringWithFormat:@"x_new_feature_%zd.jpg", i+1]];
        } else {
            imgV.image = [UIImage imageNamed:[NSString stringWithFormat:@"new_feature_%zd.jpg", i+1]];
        }
        if (i == NewFeatureCount - 1) {
            [self setupLastImageView:imgV];
        }
        [self.scrollView addSubview:imgV];
    }
}


/**
 *  初始化最后一个imageView
 *
 *  @param imageView 最后一个imageview
 */
- (void)setupLastImageView:(UIImageView *)imageView {
    // 开启交互功能
    imageView.userInteractionEnabled = YES;
    UIButton *startBtn = [[UIButton alloc] initWithFrame:CGRectMake((kScreenWidth - 160) * 0.5, kScreenHeight - 90, 160, 40)];
    [startBtn setTitle:@"开始" forState:UIControlStateNormal];
    [startBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [startBtn setBackgroundColor:kColor(81, 124, 240, 1)];
    startBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    startBtn.layer.cornerRadius = 20;
    startBtn.layer.masksToBounds = YES;
    [startBtn addTarget:self action:@selector(startClick) forControlEvents:UIControlEventTouchUpInside];
    [imageView addSubview:startBtn];
}

- (void)startClick {
    NSString *firstStartKey = [NSString stringWithFormat:@"FirstStart_%@",[Tool getSoftVersion]];
    [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:firstStartKey];
    
    UIWindow *win = [UIApplication sharedApplication].delegate.window;
  
    UserModel *user = [NSUDManager currentUser];
    if (user) {
        if ([user.role isEqualToString:@"0"] || [user.role integerValue] == 0) {
//            SelectRolesViewController *srVC = [[SelectRolesViewController alloc] init];
//            srVC.index = 1;
            RolesViewController *roles = [[RolesViewController alloc] init];
            win.rootViewController = roles;
        } else {
            [((AppDelegate *)[UIApplication sharedApplication].delegate) gotoHome];

        }
    } else {
        win.rootViewController = [[LoginViewController alloc] init];
    }
    
    [win makeKeyAndVisible];
}


@end
